package com.cl.proxy;

import java.util.Random;

public class HasQuarterState implements State {
    Random random = new Random();
    GumballMachine gumballMachine;

    public HasQuarterState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }

    @Override
    public void insertQuarter() {
        System.out.println("钱够了");
    }

    @Override
    public void ejectQuarter() {
        System.out.println("退钱了");
        gumballMachine.setState(gumballMachine.getNoQuarterState());
    }

    @Override
    public void turnCrank() {
        System.out.println("转动摇杆");
        int nextInt = random.nextInt(10);
        System.out.println(nextInt);
        if (nextInt < 5 && gumballMachine.getCount() > 1) {
            gumballMachine.setState(gumballMachine.getWinnerState());
        } else {
            gumballMachine.setState(gumballMachine.getSoldState());
        }
    }

    @Override
    public void dispense() {
        System.out.println("不支持的操作");
    }
}
